package panoramic.textures;

import java.io.File;

import javax.media.opengl.*;
import javax.media.opengl.glu.GLU;

import com.sun.opengl.util.texture.*;
import com.sun.opengl.util.*;

import java.util.HashMap;


public class TextureManager
{
	
	private HashMap<Integer, Texture> m_textures = new HashMap<Integer, Texture>();
	
	private GLU m_glu = new GLU();
	
	
	private static TextureManager ref;
	
	private boolean m_loadedTextures = false;
	
	private TextureManager()
	{
		
	}
	
	public boolean hasLoadedTextures()
	{
		return m_loadedTextures;
	}

	public static TextureManager getInstance()
	{
		if (ref == null)
			ref = new TextureManager();		
		return ref;
	}

	public Object clone() throws CloneNotSupportedException
	{
		throw new CloneNotSupportedException(); 
	}
	
	
	
	public void loadTexture(String filename, int texID, boolean generateMipMap, boolean doRepeat)
	{
		try
		{
			Texture tex = null;
			
			tex = TextureIO.newTexture(new File(filename), generateMipMap);//mipMap);
			
			if(generateMipMap)
			{
				// when texture area is small, bilinear filter the closest mipmap
				//tex.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR_MIPMAP_NEAREST);
				//tex.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
				
			    
				
			}
			else
			{
				tex.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_NEAREST);
				tex.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_NEAREST);
			}
			
			
			tex.setTexParameteri(GL.GL_TEXTURE_WRAP_S, doRepeat ? GL.GL_REPEAT : GL.GL_CLAMP);
			tex.setTexParameteri(GL.GL_TEXTURE_WRAP_T, doRepeat ? GL.GL_REPEAT : GL.GL_CLAMP);
			
						
			
			
			tex.enable();
			
			m_textures.put(texID, tex);
			
			m_loadedTextures = true;
		}
		catch(Exception e)
		{
			System.out.println(e.getMessage());
			System.out.println("Error loading texture " + filename);
		}
		
		//System.out.println( m_textures.toString() );
		
		

	}
	
	public Texture getTexture(int texID)
	{
		Texture retVal = m_textures.get(texID);
		//System.out.println("getTexture(" + texID + ")=" + retVal);
		return retVal;
	}

	

}
